import log
import service
import blue
import uix
import trinity

class DrawingSvc(service.Service):
    __guid__ = 'svc.draw'
    __update_on_reload__ = 0

    def Run(self, *etc):
        service.Service.Run(self, *etc)



    def Stop(self, *etc):
        service.Service.Stop(self, *etc)



    def GetDefaultTexture(self, clear = 0):
        t = trinity.TriTexture()
        t.pixels = ('res:/UI/Texture/%s.dds' % ['fill', 'none'][clear])
        return t



    def Background(self, where, idx = -1):
        if (idx == -1):
            idx = len(where.children)
        background = blue.os.LoadObject('res:/UI/Component/underlay_bumped_UIFrame.blue')
        background.name = 'background'
        where.children.insert(idx, background)
        return background



    def TabGroup(self, parent, tabs, groupID = None, autoSelect = 1, name = 'tabparent', callback = None, isSub = 0, detachable = 0, upsideDown = False, centered = 0, idx = -1, *args):
        import form
        tabgroup = form.TabGroup(self.Container(name, parent, align=uix.UI_ALTOP, idx=idx, state=uix.UI_PICKCHILDREN))
        tabgroup.Startup(tabs, groupID, callback, isSub, detachable, autoSelect, upsideDown, centered)
        return tabgroup



    def TransformContainer(self, name = 'transformcontainer', parent = None, align = 5, left = 0, top = 0, width = 0, height = 0, idx = -1, state = 3, isClipper = 0, position = None, lockAspect = 0, anchors = None, rotation = 0.0):
        container = blue.os.CreateInstance('triui.UITransform')
        container.name = name
        container.left = left
        container.top = top
        container.width = width
        container.height = height
        container.state = state
        container.align = align
        container.autoPos = (position or 0)
        container.clipChildren = isClipper
        if (anchors is not None):
            container.anchors = anchors
        if (lockAspect and (align not in (uix.UI_ALNONE, uix.UI_ALCLIENT))):
            uix.LogInfo('lockAspect', lockAspect)
            container.SetAspectLock(lockAspect)
        if parent:
            if not hasattr(parent, 'children'):
                log.LogTraceback('parent has no children')
                return container
            if (idx in (-1, None)):
                idx = len(parent.children)
            parent.children.insert(idx, container)
        container.transform.RotationZ(rotation)
        return container



    def Video(self, name = 'video', parent = None, path = '', align = 0, left = 0, top = 0, width = 0, height = 0, idx = -1, state = 0, position = 0, anchors = None):
        video = trinity.UIVideo()
        if path:
            video.videoPath = path
        video.name = name
        video.left = left
        video.top = top
        video.width = width
        video.height = height
        video.state = state
        video.align = align
        video.autoPos = position
        if parent:
            if (idx == -1):
                idx = len(parent.children)
            parent.children.insert(idx, video)
        return video



exports = {'draw.GetDefaultTexture': lambda *args, **kw: sm.GetService('draw').GetDefaultTexture(*args, **kw),
 'draw.TransformContainer': lambda *args, **kw: sm.GetService('draw').TransformContainer(*args, **kw),
 'draw.Background': lambda *args, **kw: sm.GetService('draw').Background(*args, **kw),
 'draw.TabGroup': lambda *args, **kw: sm.GetService('draw').TabGroup(*args, **kw),
 'draw.Video': lambda *args, **kw: sm.GetService('draw').Video(*args, **kw)}

